package vip.xiaonuo.bi.core.dataset.enums;

import cn.hutool.core.date.DateUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据集-属性 改变可选类型 Long double Date String
 *   @author 顾育司
 *   @date  2023/12/29 12:09
 */
@Getter
public enum DataSetColumnType {
    INT(){
        @Override
        protected List<String> converData(List<String> dataset) {
            List<String> collect = dataset.stream()
                    .map(item->{
                        try {
                            if(item.contains(".")){
                                return String.valueOf((int) Double.parseDouble(item));
                            }
                            return String.valueOf(Integer.parseInt(item));
                        }catch (Exception e){
                            return null;
                        }})
                    .collect(Collectors.toList());
          return collect;
        }
    },
    STRING(){
        @Override
        protected List<String> converData(List<String> dataset) {
            return dataset;
        }
    },
    DOUBLE(){
        @Override
        protected List<String> converData(List<String> dataset) {
            List<String> collect = dataset.stream()
                    .map(item->{
                        try {
                             return String.valueOf(Double.parseDouble(item));
                        }catch (Exception e){
                            return null;
                        }})
                    .collect(Collectors.toList());
            return collect;
        }
    },
    DATE(){
        @Override
        protected List<String> converData(List<String> dataset) {
            List<String> collect = dataset.stream()
                    .map(item->{
                        try {
                          return   String.valueOf(DateUtil.format(DateUtil.parse(item), "yyyy/MM/dd"));
                        }catch (Exception e){
                            return null;
                        }})
                    .collect(Collectors.toList());
            return collect;
        }
    },
    TIMESTAMP(){
        @Override
        protected List<String> converData(List<String> dataset) {
            List<String> collect = dataset.stream()
                    .map(item->{
                        try {
                            return   String.valueOf(DateUtil.parse(item));
                        }catch (Exception e){
                            return null;
                        }})
                    .collect(Collectors.toList());
            return collect;
        }
    },
    BOOLEAN(){
        @Override
        protected List<String> converData(List<String> dataset) {
            return dataset;
        }
    },
    LONG(){
        @Override
        protected List<String> converData(List<String> dataset) {
            List<String> collect = dataset.stream()
                    .map(item->{
                        try {
                            return String.valueOf(Long.parseLong(item));
                        }catch (Exception e){
                            return null;
                        }})
                    .collect(Collectors.toList());
            return collect;
        }
    },
    SHORT(){
        @Override
        protected List<String> converData(List<String> dataset) {
            List<String> collect = dataset.stream()
                    .map(item->{
                        try {
                            if(item.contains(".")){
                                return String.valueOf((int) Double.parseDouble(item));
                            }
                            return String.valueOf(Integer.parseInt(item));
                        }catch (Exception e){
                            return null;
                        }})
                    .collect(Collectors.toList());
            return collect;
        }
    },
    FLOAT(){
        @Override
        protected List<String> converData(List<String> dataset) {
            List<String> collect = dataset.stream()
                    .map(item->{
                        try {
                            return String.valueOf(Float.parseFloat(item));
                        }catch (Exception e){
                            return null;
                        }})
                    .collect(Collectors.toList());
            return collect;
        }
    },
    ;

    protected abstract List<String> converData(List<String> dataset);

    public static DataSetColumnType getType(String type){
        return  valueOf(type.toUpperCase());
    }

    public static void main(String[] args) {
        // 测试
        List<String> doubleList = Arrays.asList("1.23", "4","65979376974036707", "ern","2017-01-01");
        DataSetColumnType anInt = DataSetColumnType.getType("double");
        System.out.println(anInt.converData(doubleList));
    }
}
